package com.hp.attendance.service;

import com.hp.attendance.dao.RoleDao;
import com.hp.attendance.entity.Menu;
import com.hp.attendance.entity.PageInfo;
import com.hp.attendance.entity.Role;
import com.hp.attendance.entity.RoleMenu;
import com.hp.attendance.entity.dto.MenuDto;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleService {
    private RoleDao roleDao = new RoleDao();

    /**
     * 查询所有角色的列表
     * @return
     */
    public List<Role> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return roleDao.getList();
    }

    /**
     * 分页查询角色列表
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public PageInfo<Role> getPage(int pageNo , int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        PageInfo<Role> page = new PageInfo<Role>();
        //查询分页的数据                           2       10
        List<Role> list = roleDao.getLimitList(pageNo, pageSize);
        page.setRecords(list);
        //查询总记录条数
        long total = roleDao.getTotal();
        page.setTotal(total);
        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }

    /**
     * 添加角色
     * @param role
     */
    public void addRole(Role role) throws SQLException, IOException, ClassNotFoundException {
        roleDao.addRole(role);
    }


    /**
     * 根据id查询role对象
     * @param id  role的主键id值
     * @return
     */
    public Role getRole(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return roleDao.getRole(id);
    }

    /**
     * 修改角色
     * @param role
     */
    public void updateRole(Role role) throws SQLException, IOException, ClassNotFoundException {
       roleDao.updateRole(role);
    }

    /**
     * 删除角色
     * @param id
     */
    public void delRole(int id) throws SQLException, IOException, ClassNotFoundException {
        roleDao.delRole(id);
    }

    //执行分配菜单的功能
    public void dittriMenu(List<RoleMenu> listrm,int roleId){
        try {
            //开启事务
            roleDao.delRoleMenu(roleId);
            roleDao.addRoleMenu(listrm);
            //提交事务
        } catch (Exception throwables) {
            //回滚事务
            throwables.printStackTrace();
        }
    }

    /**
     * 根据员工id查询出所对应角色列表
     * @param empId
     * @return
     */
    public List<Role> getRoleByEmpId(int empId) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return roleDao.getRoleByEmpId(empId);
    }
}
